import time
import unittest
from Util.LoginUtil import login_util


class Login_test(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.driver = login_util("chrome")
        cls.driver.request("http://39.101.167.251/xinhu/")
        time.sleep(3)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.qt()

    # 用户名为空
    def test_01(self):
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[1]/div/input', '')
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[3]/div/input', 'qf123456')
        self.driver.click('xpath', '/html/body/div/div[4]/form/div[4]/button')
        time.sleep(3)
        self.driver.upclear('xpath', '//*[@id="loginview0"]/div[1]/div/input')
        self.driver.upclear('xpath', '//*[@id="loginview0"]/div[3]/div/input')
        usernull = self.driver.getElement('xpath', '//*[@id="msgview"]/span').text
        self.assertEqual('用户名不能为空', usernull, msg='预期结果与实际值不符')

    # 密码不能为空
    def test_02(self):
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[1]/div/input', 'admin')
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[3]/div/input', '')
        self.driver.click('xpath', '/html/body/div/div[4]/form/div[4]/button')
        time.sleep(3)
        self.driver.upclear('xpath', '//*[@id="loginview0"]/div[1]/div/input')
        self.driver.upclear('xpath', '//*[@id="loginview0"]/div[3]/div/input')
        usernull = self.driver.getElement('xpath', '//*[@id="msgview"]/span').text
        self.assertEqual('密码不能为空', usernull, msg='预期结果与实际值不符')

    # 用户不存在
    def test_03(self):
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[1]/div/input', 'admin1')
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[3]/div/input', 'qf123456')
        self.driver.click('xpath', '/html/body/div/div[4]/form/div[4]/button')
        time.sleep(3)
        self.driver.upclear('xpath', '//*[@id="loginview0"]/div[1]/div/input')
        self.driver.upclear('xpath', '//*[@id="loginview0"]/div[3]/div/input')
        usernull = self.driver.getElement('xpath', '//*[@id="msgview"]/span').text
        self.assertEqual('用户不存在', usernull, msg='预期结果与实际值不符')

    # 密码不正确
    def test_04(self):
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[1]/div/input', 'admin')
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[3]/div/input', 'qf12345612')
        self.driver.click('xpath', '/html/body/div/div[4]/form/div[4]/button')
        time.sleep(3)
        self.driver.upclear('xpath', '//*[@id="loginview0"]/div[1]/div/input')
        self.driver.upclear('xpath', '//*[@id="loginview0"]/div[3]/div/input')
        usernull = self.driver.getElement('xpath', '//*[@id="msgview"]/span').text
        self.assertEqual('密码不对', usernull, msg='预期结果与实际值不符')

    # 用户登录
    def test_05(self):
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[1]/div/input', 'admin')
        self.driver.send_keys('xpath', '//*[@id="loginview0"]/div[3]/div/input', 'qf123456')
        self.driver.click('xpath', '/html/body/div/div[4]/form/div[4]/button')
        time.sleep(3)
        usernull = self.driver.getElement('xpath', '//*[@id="app-navbar-collapse"]/ul[2]/li[2]/a/span').text
        self.assertEqual('REIM', usernull, msg='预期结果与实际值不符')


if __name__ == '__main__':
    unittest.main
